<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class JSON extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('JSON_model','m');
	  $this->load->library('session');
    $this->load->helper('url');
  }

  public function index()
  {
  }
  
  public function tree()
  {
    $data['kecamatan']=$this->m->tree();
    $this->load->view('json/tree',$data);
  }

  //init data kelurahan yang akan tampil di data penel Home
  public function kelurahan()
  {
    $data['kelurahan']=$this->m->kelurahan();
    $this->load->view('json/kelurahan',$data);
  }

  public function test($id=1)
  {
	var_dump($this->m->getShape( (int) $this->m->getKelurahanPolygonId($id)));
  }
  
  public function shape($id){
	header('Content-Type: application/javascript');
  	$ar_shape = preg_split('~\R~',$this->m->getShape((int) $this->m->getKelurahanPolygonId($id)));
	echo 'var kel'.(int)$id." = [[";
	echo implode("],[",$ar_shape);
	echo ']];';

  }
  
  public function shape_all(){
	header('Content-Type: application/javascript');
	//var_dump ($this->m->getAllId('polygon'));
	//ternyata bukan polygon, tapi seharusnya kelurahan
	$all_polygon_id = $this->m->getAllId('kelurahan');
	foreach($all_polygon_id as $id){
		echo "\n\n";
		$ar_shape = preg_split('~\R~',$this->m->getShape((int) $this->m->getKelurahanPolygonId($id->id)));
		echo 'var kelurahan'.(int)$id->id."_ar = [[";
		echo implode("],[",$ar_shape);
		echo ']];';
	}
  }

}
